Skip to content

Fix: deal with bigint types in objects stored as jsonb#2947

Closed
ales-albert-kilbergr wants to merge 1 commit intobrianc:masterfrom
ales-albert-kilbergr:fix/binint-prop-jsonb-column
Closed

Fix: deal with bigint types in objects stored as jsonb#2947
ales-albert-kilbergr wants to merge 1 commit intobrianc:masterfrom
ales-albert-kilbergr:fix/binint-prop-jsonb-column

Conversation

@ales-albert-kilbergr
Copy link
Copy Markdown

@ales-albert-kilbergr ales-albert-kilbergr commented Apr 1, 2023

The pg library fails to serialize query values in case when an object for jsonb column contains bigint.

The failed error message is as follows because JSON.stringify does not know how to deal with bigint.

TypeError: Do not know how to serialize a BigInt

Reproducible example could look like this:

const queryConfig = `INSERT INTO my_table (id, data) VALUES ($1, $2)`;
const values = ['someId', { someValue: BigInt(1) }]
await pgClient.query(queryConfig, values);

Currently a pg library fails when processing objects for jsonb columns which contains bigints because JSON.stringify by default does not know how to deal with it.
@charmander
Copy link
Copy Markdown
Collaborator

This introduces overhead to every JSON encoding in order to accommodate one specific type. I would suggest moving the JSON.stringify step to user code when you need to stringify a BigInt; I don’t think it belongs in pg.

Also, I wouldn’t expect BigInt to be serialized as a JSON string.

@ales-albert-kilbergr
Copy link
Copy Markdown
Author

ales-albert-kilbergr commented Apr 4, 2023

Also, I wouldn’t expect BigInt to be serialized as a JSON string.

A big int can exceed a range for number in javascript therefor there is not much other options how to serialize it in JSON than a string currently.

I would suggest moving the JSON.stringify step to user code when you need to stringify a BigInt

That would be a good option.

I don’t think it belongs in pg.

BigInt is standartized javascript primitive type. Why shouldn`t be possible to pass it as a property for JSONB object?

@charmander
Copy link
Copy Markdown
Collaborator

BigInt is standartized javascript primitive type. Why shouldn`t be possible to pass it as a property for JSONB object?

For the reasons I mentioned. To expand on the second one: it doesn’t survive the round trip through JSON.serialize. To emphasize the first: this affects all JSON serialization and is unrelated to pg, so why should pg have its own special behaviour?

@charmander charmander closed this Jan 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants